package com.qianduo.util;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import com.qianduo.vo.AccountForm;
import com.qianduo.vo.CitiesForm;

public class Common {
	public static String getSessionAccount(HttpServletRequest req){
		AccountForm account = (AccountForm)req.getSession(false).getAttribute(Constants.ACCOUNT);
		return account.getUsername();
	}
	
	public static String getProvinceID(String cityID){
		String provinceID	= "";
		if(null == cityID || "".equals(cityID)){return provinceID;}
		Map<String,List<CitiesForm>> map	= Global.citiesMap;
		for(Entry<String,List<CitiesForm>> entry : map.entrySet()){
			List<CitiesForm> list	= entry.getValue();
			for(CitiesForm cf : list){
				String tmpCityID		= cf.getId().toString();
				String tmpProvinceID	= cf.getPid().toString();
				if(cityID.equals(tmpCityID)){
					provinceID	= tmpProvinceID;
					break;
				}
			}
		}
		return provinceID;
	}
	
	public static String getProvinceName(String cityID){
		String provinceName	= "";
		if(null == cityID || "".equals(cityID)){return provinceName;}
		Map<String,List<CitiesForm>> map	= Global.citiesMap;
		for(Entry<String,List<CitiesForm>> entry : map.entrySet()){
			List<CitiesForm> list	= entry.getValue();
			for(CitiesForm cf : list){
				String tmpCityID		= cf.getId().toString();
				String tmpProvinceName	= cf.getProvince();
				if(cityID.equals(tmpCityID)){
					provinceName	= tmpProvinceName;
					break;
				}
			}
		}
		return provinceName;
	}
	
	public static String getCityName(String cityID){
		String cityName	= "";
		if(null == cityID || "".equals(cityID)){return cityName;}
		Map<String,List<CitiesForm>> map	= Global.citiesMap;
		for(Entry<String,List<CitiesForm>> entry : map.entrySet()){
			List<CitiesForm> list	= entry.getValue();
			for(CitiesForm cf : list){
				String tmpCityID	= cf.getId().toString();
				String tmpCityName	= cf.getCityName();
				if(cityID.equals(tmpCityID)){
					cityName	= tmpCityName;
					break;
				}
			}
		}
		return cityName;
	}
}
